package labox.innovation.db.proxy;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.db.manager.DBConnetionStateManager;

import com.mysql.jdbc.exceptions.jdbc4.CommunicationsException;

/**
 * SQLException ������
 * @author lian
 *
 */
public class SQLExceptionHandler {
	private static Logger _log = LoggerFactory.getLogger(SQLExceptionHandler.class.getName());

	/**
	 * �����ڻ�ȡ�������ʱ���쳣
	 * @param e �쳣
	 *
	 */
	public static void handlingConnection(SQLException e) {
		if (e instanceof CommunicationsException) {
			_log.error("������ݿⳬʱ", Config.SERVER_ID, e);
			DBConnetionStateManager.startResumeDbConnection();
		} else
			e.printStackTrace();
	}

	/**
	 * ������ִ��executeQuery ,executeUpdateʱ������쳣
	 * @param e
	 * @param str
	 * @throws SQLException
	 */
	public static void handlingExecute(SQLException e, String str) throws SQLException {
		if (e instanceof CommunicationsException) {
			_log.error(str + "��ݿ����ӶϿ�", Config.SERVER_ID, e);
			DBConnetionStateManager.startResumeDbConnection();
		} else
			throw e;
	}

	/**
	 * ���ָ���쳣�Ƿ�������Ͽ�����
	 * @param e
	 * @return 
	 */
	public static boolean isDisConnection(SQLException e) {
		if (e instanceof CommunicationsException) {
			return true;
		}
		return false;
	}
}
